package com.rem.designPattern.Command命令;

/**
 * 命令角色
 *
 * @author Rem
 * @date 2022-11-19
 */
public interface Command {

    /**
     * 执行
     */
    void execute();

    /**
     * 撤销
     */
    void undo();
}

/**
 * 空命令
 */
class NonCommand implements Command {
    @Override
    public void execute() {
    }

    @Override
    public void undo() {
    }
}

/**
 * 开灯的命令
 */
class LightOnCommand implements Command {
    private final LightReceiver light;

    public LightOnCommand(LightReceiver light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();
    }

    @Override
    public void undo() {
        light.off();
    }
}

/**
 * 关灯的命令
 */
class LightOffCommand implements Command {
    private final LightReceiver light;

    public LightOffCommand(LightReceiver light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();
    }

    @Override
    public void undo() {
        light.on();
    }
}

/**
 * 打开电视的命令
 */
class TVOnCommand implements Command {
    private final TVReceiver tv;

    public TVOnCommand(TVReceiver tv) {
        this.tv = tv;
    }

    @Override
    public void execute() {
        tv.on();
    }

    @Override
    public void undo() {
        tv.off();
    }
}

/**
 * 关闭电视的命令
 */
class TVOffCommand implements Command {
    private final TVReceiver tv;

    public TVOffCommand(TVReceiver tv) {
        this.tv = tv;
    }

    @Override
    public void execute() {
        tv.off();
    }

    @Override
    public void undo() {
        tv.on();
    }
}